package dd.prog2.exercises.set13.mvc_gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Panel;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class View_c4 extends JFrame  implements Observer {

	private Model_c4 model;
	
	public final static JButton reset = new JButton("Start new game");
	public final static JButton ai_turn = new JButton("Propose");
	
	private JTextField status = new JTextField("Status: waiting for the player 1 turn");
	private JTextArea info = new JTextArea(("\nRED: Player 1\nYELLOW: Player 2"));
	
//	private JFrame frame;
	private Panel buttonPanel;
	
	public View_c4(Model_c4 model, Controller_c4 cont) {
		// TODO Auto-generated constructor stub
		this.model = model;
//		cont.setView(this);
		
		setTitle("Vier gewinnt v3 MVC");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(null);
		setLocation(250, 250);
		setResizable(false);
		setSize(305, 370);
		
		buttonPanel = new Panel(); // buttons
		buttonPanel.setLayout(new GridLayout(6, 7));
		buttonPanel.setSize(305, 250);
		buttonPanel.setLocation(0, 0);
		buttonPanel.setBackground(Color.GREEN);
		// //////////////////////////////////
		for (int j = 0; j < 42; j++) {
			Coin coin = new Coin(j);
			coin.addActionListener(cont);
			buttonPanel.add(coin);
		}
		model.reset();
		
		status.setLocation(0, 245); // textfield
		status.setSize(305, 20);
		status.setEditable(false);
		status.setHorizontalAlignment(JTextField.LEFT);

		reset.setLocation(0, 265); // reset button
		reset.setSize(210, 25);
		reset.addActionListener(cont);
		
		ai_turn.setLocation(210, 265); // ai button
		ai_turn.setSize(90, 25);
		ai_turn.addActionListener(cont);
		
		info.setLocation(0, 290); // textfield
		info.setSize(305, 60);
		info.setEditable(false);

		add(status);
		add(buttonPanel);
		add(reset);
		add(ai_turn);
		add(info);

		update(null, null);
	}

	@Override
	public void update(Observable o, Object arg){
		buttonPanel.repaint();
//		System.out.println("Status: waiting for the player "+model.getPlayer()+" turn");
		status.setText("Status: waiting for the player "+model.getPlayer()+" turn");
		if(model.gameOver){
			if(model.winner != 0)
				status.setText("Status: Player "+model.winner+" wins");
			else
				status.setText("Status: Game Over. No winner");
		}
	}
	
	class Coin extends JButton {
		int index;
		
		public Coin(int index) {
			this.index = index;
			setBackground(Color.GREEN);
		}

		// Paint the round background
		protected void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.setColor(Color.GREEN);
			Model_c4 m = View_c4.this.model;
			if(m.getCoinAt(index) != 0){
				g.setColor(m.getCoinAt(index) == 1 ? Color.RED : Color.YELLOW);
//				System.out.println(m.getCoinAt(index));
			}
			g.fillOval(0, 0, getSize().width - 2, getSize().height - 2);

		}
	
	}
}
